package org.wheel.jpa.boot.internal;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.spi.MetadataSourcesContributor;
import org.wheel.base.stmt.OrmSqlUtils;

/**
 * 扩展 MetadataSources
 * 
 * @author klaus.wang
 *
 */
public class WebMetadataSourcesContributor implements MetadataSourcesContributor {

	@Override
	public void contribute(MetadataSources metadataSources) {
		OrmSqlUtils.callEntities((strs) -> {
			if (StringUtils.isBlank(strs)) {
				return;
			}
			InputStream is = new ByteArrayInputStream(
					String.format("<hibernate-mapping>\n%s\n</hibernate-mapping>", strs).getBytes());

			metadataSources.addInputStream(is);
		});
	}

}
